#include <ogcsys.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "prompts/promptwindows.h"
#include "file/fileops.h"
#include "main.h"

char deviceRoot[10];

Device Device;
CFG cfg;

Settings::Settings()
{
}

Settings::~Settings()
{
}

void Settings::SetDefault()
{	
	strcpy(deviceRoot, "sd:/");
	if (device.ConfigDevice == 2)
		strcpy(deviceRoot, "usb:/");
	
	sprintf(cfg.SaveGamePath, "%s%s", deviceRoot, SAVEPATH);
    sprintf(cfg.SaveMiisPath, "%s%s", deviceRoot, MIIPATH);
    sprintf(cfg.CustomFont, "%s%sfont.ttf", deviceRoot, CONFIGPATH);
    strcpy(cfg.LanguageFile, "NotSet");
    sprintf(cfg.LanguagePath, "%s%s", deviceRoot, LANGPATH);
    sprintf(cfg.UpdatePath, "%s%s", deviceRoot, UPDATEPATH);
    sprintf(cfg.BgMusicPath, "%s%s", deviceRoot, MUSICPATH);
    cfg.BgMusicLoop = 1;
	cfg.BootMethod = 0;
	cfg.MusicVolume = 80;
	cfg.CreditsVolume = 50;
	cfg.SFXVolume = 80;
	cfg.Clock = 1;
	cfg.Tooltips = 1;
	cfg.InfoWindow = 0;
	cfg.Screensaver = 3;
	cfg.Rumble = 1;
	cfg.Widescreen = 0;
	cfg.AutoConnect = 0;
	cfg.UpdateMeta = 0;
	cfg.UpdateIcon = 0;
	cfg.UpdateWiiTdb = 0;
	cfg.WiiTdbTitles = 0;
	cfg.WiiTdbLanguage = -1;
	cfg.WiiTdb_JPtoEN = 0;
    sprintf(cfg.WiiTdbPath, "%s%s", deviceRoot, CONFIGPATH);
    strcpy(cfg.WiiTdbLanguageCode, "NotSet");
    
}

bool Settings::Save()
{
    strcpy(deviceRoot, "sd:/");
	if (device.ConfigDevice == 2)
		strcpy(deviceRoot, "usb:/");
	
	char filepath[300];
    char folder[300];
    snprintf(filepath, sizeof(filepath), "%s%s%s", deviceRoot, CONFIGPATH, CONFIGNAME);
	snprintf(folder, sizeof(folder), "%s%s", deviceRoot, CONFIGPATH);

	CreateSubfolder(folder);
    file = fopen(filepath, "w");

    if(!file) {
        fclose(file);
        return false;
    }
	
    fprintf(file, "# SaveGame_Manager_GX Settingsfile\n");
	fprintf(file, "# Note: This file is automatically generated\n\n");
	
	fprintf(file, "# Gui Settings\n");
	
	fprintf(file, "BootMethod = %d\n", cfg.BootMethod);
	fprintf(file, "Clock = %d\n", cfg.Clock);
	fprintf(file, "Tooltips = %d\n", cfg.Tooltips);
	fprintf(file, "InfoWindow = %d\n", cfg.InfoWindow);
	fprintf(file, "Screensaver = %d\n", cfg.Screensaver);
	fprintf(file, "Rumble = %d\n", cfg.Rumble);
	fprintf(file, "Widescreen = %d\n\n", cfg.Widescreen);
	
	fprintf(file, "# Sound Settings\n");
	
	fprintf(file, "MusicVolume = %d\n", cfg.MusicVolume);
	fprintf(file, "CreditsVolume = %d\n", cfg.CreditsVolume);
	fprintf(file, "SFXVolume = %d\n", cfg.SFXVolume);
	fprintf(file, "BgMusicLoop = %d\n", cfg.BgMusicLoop);
	fprintf(file, "BgMusicPath = %s\n\n", cfg.BgMusicPath);
    
	fprintf(file, "# Path Settings\n");
	
	fprintf(file, "LanguageFile = %s\n", cfg.LanguageFile);
	fprintf(file, "LanguagePath = %s\n",cfg.LanguagePath);
	fprintf(file, "SaveGamePath = %s\n", cfg.SaveGamePath);
	fprintf(file, "SaveMiisPath = %s\n", cfg.SaveMiisPath);
	fprintf(file, "CustomFont = %s\n", cfg.CustomFont);
	fprintf(file, "UpdatePath = %s\n",cfg.UpdatePath);
	fprintf(file, "WiiTdbPath = %s\n\n",cfg.WiiTdbPath);
	
	fprintf(file, "# Update Settings\n");
	
	fprintf(file, "UpdateMeta = %d\n", cfg.UpdateMeta);
	fprintf(file, "UpdateIcon = %d\n",cfg.UpdateIcon);
	fprintf(file, "UpdateWiiTdb = %d\n",cfg.UpdateWiiTdb);
	fprintf(file, "AutoConnect = %d\n\n", cfg.AutoConnect);
	
	fprintf(file, "# WiiTdb Settings\n");
	
	fprintf(file, "WiiTdbTitles = %d\n", cfg.WiiTdbTitles);
	fprintf(file, "WiiTdb_JPtoEN = %d\n", cfg.WiiTdb_JPtoEN);
	fprintf(file, "WiiTdbLanguage = %d\n",cfg.WiiTdbLanguage);
	fprintf(file, "WiiTdbLanguageCode = %s\n",cfg.WiiTdbLanguageCode);
	
	
	fclose(file);

	return true;
}

bool Settings::Load()
{
	strcpy(deviceRoot, "sd:/");
	if (device.ConfigDevice == 2)
		strcpy(deviceRoot, "usb:/");
	
	char line[300];
    char filepath[300];
    snprintf(filepath, sizeof(filepath), "%s%s%s", deviceRoot, CONFIGPATH, CONFIGNAME);

	file = fopen(filepath, "r");
	if (!file) {
		return false;
	}
	while (fgets(line, sizeof(line), file)) {

		if (line[0] == '#') continue;

        this->ParseLine(line);
	}
	fclose(file);

	return true;

}

bool Settings::Init()
{
	if (!Device.Init())
	{
		SetDefault();
		return false;
	}
	
	SetDefault();
	Load();
	Save();	
	
	return true;
}

bool Settings::LoadLanguage(const char *path, int language)
{
	bool ret = false;
	
	if(language >= 0 || !path) {

        if(language < 0)
			return false;
			
        char filepath[300];

        if(language == APP_DEFAULT) {
            strcpy(cfg.LanguageFile, "Default");
            gettextCleanUp();
			Save();
            return true;
        }
        else if(language == CONSOLE_DEFAULT) {
            gettextCleanUp();
            return this->LoadLanguage(NULL, CONF_GetLanguage()+2);
		}
        else if(language == JAPANESE) {
            snprintf(filepath, sizeof(filepath), "%sjapanese.lang", cfg.LanguagePath);
        }
        else if(language == ENGLISH) {
            snprintf(filepath, sizeof(filepath), "%senglish.lang", cfg.LanguagePath);
        }
        else if(language == GERMAN) {
            snprintf(filepath, sizeof(filepath), "%sgerman.lang", cfg.LanguagePath);
        }
        else if(language == FRENCH) {
            snprintf(filepath, sizeof(filepath), "%sfrench.lang", cfg.LanguagePath);
        }
        else if(language == SPANISH) {
            snprintf(filepath, sizeof(filepath), "%sspanish.lang", cfg.LanguagePath);
        }
        else if(language == ITALIAN) {
            snprintf(filepath, sizeof(filepath), "%sitalian.lang", cfg.LanguagePath);
        }
        else if(language == DUTCH) {
            snprintf(filepath, sizeof(filepath), "%sdutch.lang", cfg.LanguagePath);
        }
        else if(language == S_CHINESE) {
            snprintf(filepath, sizeof(filepath), "%ss_chinese.lang", cfg.LanguagePath);
        }
        else if(language == T_CHINESE) {
            snprintf(filepath, sizeof(filepath), "%st_chinese.lang", cfg.LanguagePath);
        }
        else if(language == KOREAN) {
            snprintf(filepath, sizeof(filepath), "%skorean.lang", cfg.LanguagePath);
        }

        ret = gettextLoadLanguage(filepath);
        if(ret)
		{
			char *languagefile = strrchr(filepath, '/')+1;
			strcpy(cfg.LanguageFile, languagefile);
			Save();
			return ret;
		}
		
    } else {
		ret = gettextLoadLanguage(path);
		Save();
    }

    return ret;
}

bool Settings::Reset()
{
    this->SetDefault();

    if(this->Save())
        return true;

	return false;
}

bool Settings::SetSetting(char *name, char *value)
{
    int i = 0;

    if (strcmp(name, "LanguageFile") == 0) {
        strncpy(cfg.LanguageFile, value, sizeof(cfg.LanguageFile));
		return true;
	}
	else if (strcmp(name, "LanguagePath") == 0) {
        strncpy(cfg.LanguagePath, value, sizeof(cfg.LanguagePath));
		return true;
	}
	else if (strcmp(name, "CustomFont") == 0) {
        strncpy(cfg.CustomFont, value, sizeof(cfg.CustomFont));
		return true;
	}
	else if (strcmp(name, "SaveGamePath") == 0) {
        strncpy(cfg.SaveGamePath, value, sizeof(cfg.SaveGamePath));
		return true;
	}
	else if (strcmp(name, "SaveMiisPath") == 0) {
        strncpy(cfg.SaveMiisPath, value, sizeof(cfg.SaveMiisPath));
		return true;
	}
	else if (strcmp(name, "UpdatePath") == 0) {
        strncpy(cfg.UpdatePath, value, sizeof(cfg.UpdatePath));
		return true;
	}
	else if (strcmp(name, "WiiTdbPath") == 0) {
        strncpy(cfg.WiiTdbPath, value, sizeof(cfg.WiiTdbPath));
		return true;
	}
	else if (strcmp(name, "WiiTdbLanguageCode") == 0) {
        strncpy(cfg.WiiTdbLanguageCode, value, sizeof(cfg.WiiTdbLanguageCode));
		return true;
	}
	else if (strcmp(name, "BgMusicPath") == 0) {
        strncpy(cfg.BgMusicPath, value, sizeof(cfg.BgMusicPath));
		return true;
	}
	else if (strcmp(name, "BootMethod") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			cfg.BootMethod = i;
		}
		return true;
	}
	else if (strcmp(name, "MusicVolume") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			cfg.MusicVolume = i;
		}
		return true;
	}
	else if (strcmp(name, "CreditsVolume") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			cfg.CreditsVolume = i;
		}
		return true;
	}
	else if (strcmp(name, "SFXVolume") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			cfg.SFXVolume = i;
		}
		return true;
	}
	else if (strcmp(name, "BgMusicLoop") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			cfg.BgMusicLoop = i;
		}
		return true;
	}
	else if (strcmp(name, "Clock") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			cfg.Clock = i;
		}
		return true;
	}
	else if (strcmp(name, "Tooltips") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			cfg.Tooltips = i;
		}
		return true;
	}
	else if (strcmp(name, "InfoWindow") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			cfg.InfoWindow = i;
		}
		return true;
	}
	else if (strcmp(name, "Screensaver") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			cfg.Screensaver = i;
		}
		return true;
	}
	else if (strcmp(name, "Rumble") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			cfg.Rumble = i;
		}
		return true;
	}
	else if (strcmp(name, "Widescreen") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			cfg.Widescreen = i;
		}
		return true;
	}
	else if (strcmp(name, "WiiTdbTitles") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			cfg.WiiTdbTitles = i;
		}
		return true;
	}
	else if (strcmp(name, "WiiTdb_JPtoEN") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			cfg.WiiTdb_JPtoEN = i;
		}
		return true;
	}
	else if (strcmp(name, "WiiTdbLanguage") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			cfg.WiiTdbLanguage = i;
		}
		return true;
	}
	else if (strcmp(name, "UpdateMeta") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			cfg.UpdateMeta = i;
		}
		return true;
	}
	else if (strcmp(name, "UpdateIcon") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			cfg.UpdateIcon = i;
		}
		return true;
	}
	else if (strcmp(name, "UpdateWiiTdb") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			cfg.UpdateWiiTdb = i;
		}
		return true;
	}
	else if (strcmp(name, "AutoConnect") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			cfg.AutoConnect = i;
		}
		return true;
	}
	
    return false;
}

void Settings::ParseLine(char *line)
{
    char temp[200], name[200], value[200];

    memcpy (temp,line,strlen(line)+1);

    char * eq = strchr(temp, '=');

    if(!eq) return;

    *eq = 0;

    this->TrimLine(name, temp, sizeof(name));
    this->TrimLine(value, eq+1, sizeof(value));

	this->SetSetting(name, value);
}

void Settings::TrimLine(char *dest, char *src, int size)
{
	int len;
	while (*src == ' ') src++;
	len = strlen(src);
	while (len > 0 && strchr(" \r\n", src[len-1])) len--;
	if (len >= size) len = size-1;
	strncpy(dest, src, len);
	dest[len] = 0;
}
